package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.FollowStudent;
import cn.wolfcode.crm.mapper.FollowStudentMapper;
import cn.wolfcode.crm.mapper.PotentialCustomerMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IFollowStudentService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FollowStudentServiceImpl implements IFollowStudentService {

    @Autowired
    private FollowStudentMapper followStudentMapper;
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;

    public void deleteByPrimaryKey(Long id) {
        followStudentMapper.deleteByPrimaryKey(id);
    }

    public void insert(FollowStudent followStudent) {
        //获得当前时间
        followStudent.setFollowTime(new Date());
        //获得当前操作员工
        Employee curentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        followStudent.setFollowPerson(curentUser);

        followStudentMapper.insert(followStudent);
        //保存的同时修改潜在客户的值
        potentialCustomerMapper.updateByFollowStudentPotentialCustomer(followStudent.getPotentialcustomer());
    }

    public FollowStudent selectByPrimaryKey(Long id) {
        return followStudentMapper.selectByPrimaryKey(id);
    }

    public List<FollowStudent> selectAll() {
        return followStudentMapper.selectAll();
    }

    public void updateByPrimaryKey(FollowStudent followStudent) {
        followStudentMapper.updateByPrimaryKey(followStudent);
        //保存的同时修改潜在客户的值
        potentialCustomerMapper.updateByFollowStudentPotentialCustomer(followStudent.getPotentialcustomer());
    }

    public PageResult queryListByQo(QueryForObject qo) {
        int total = followStudentMapper.getTotalByQo(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<FollowStudent> rows = followStudentMapper.getListByQo(qo);
        return new PageResult(total, rows);
    }

    //审核
    public void audit(FollowStudent followStudent) {
        //修改状态
        followStudent.setState(FollowStudent.AUDITED);
        followStudentMapper.audit(followStudent);
    }

    public int queryFollowNumberByCustomerId(Long id) {
        return followStudentMapper.queryFollowNumberByCustomerId(id);
    }

    public FollowStudent queryFollowByCustomerId(Long id) {
        List<FollowStudent> list = followStudentMapper.queryFollowByCustomerId(id);
        return list.get(0);
    }
}
